{include file="my/header"}
<div class="container-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格数据查询块 -->
            <div class="table-query">
                <form class="layui-form layui-inline J-search-form" action="">
                    {if session('userclass') == '10'}
                    <div class="layui-inline">
                        <label class="layui-form-label">课程:</label>
                        <div class="layui-inline">
                            <input class="layui-input J-query-params" name="coursename" autocomplete="off" placeholder="请输入课程" />
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">姓名:</label>
                        <div class="layui-inline">
                            <input class="layui-input J-query-params" name="name" autocomplete="off" placeholder="请输入内容" />
                        </div>
                    </div>
                    {/if}

                    <div class="layui-inline">
                        <label class="layui-form-label">学期:</label>
                        <div class="layui-inline">
                            <select name="term" class="layui-input J-query-params">
                                <option value="">全部</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                            </select>
                        </div>
                    </div>
                </form>
                <button class="layui-btn J-search" data-type="reload">搜索</button>
                <button class="layui-btn J-reload">重置</button>
            </div>
            <!-- 表格 -->
            <table class="layui-hide" id="testTable" lay-filter="tableOption"></table>
            <!-- 表格工具 -->

            <script type="text/html" id="barDemo">
                <span class="btn-tips ">对成绩有疑问？</span>
                <a class="layui-btn layui-btn-xs" lay-event="modifScore">申请修改成绩</a>
                <!-- <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a> -->
            </script>
            <script type="text/html" id="toolbarDemo">
            </script>
            <div class="align-right">
                平均绩点：<span class="J-GPA">0</span>
            </div>
        </div>

        <!-- 弹出模板框 -->
        <div class="margin-top30 J-temple is-hidden">
            <form class="layui-form layui-fluid J-add-form" lay-filter="formTest">
                <input type="hidden" class="form-control" id="id" name="scoreid" value="" placeholder="请输入内容" />
                <input type="hidden" class="form-control" id="schoolid" name="schoolid" value="99" placeholder="请输入内容" />
                <div class="layui-form-item">
                    <label class="layui-form-label">原成绩</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" name="oldscore" placeholder="请输入内容" />
                    </div>
                </div>

                <!-- <div class="layui-form-item">
                    <label class="layui-form-label">修改后成绩</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" name="newscore" id="newscore" placeholder="请输入内容" />
                    </div>
                </div> -->

                <div class="layui-form-item is-hidden">
                    <label class="layui-form-label">学生</label>
                    <div class="layui-input-block">
                        <select name="studentid" id="studentid">
                            {volist name='studentsData' id='studentsData'}
                            <option value="{$studentsData.id}">{$studentsData.studentname}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">申请事项</label>
                    <div class="layui-input-block">
                        <textarea name="applyitem" placeholder="请输入内容" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item is-hidden">
                    <label class="layui-form-label">授课老师</label>
                    <div class="layui-input-block">
                        <select name="teacherid" id="teacherid">
                            {volist name='teacherinfodata' id='teacherinfodata'}
                            <option value="{$teacherinfodata.teacherid}">{$teacherinfodata.teachername}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    var studentScores;
    $(function () {
        initClass('courselist');
        layuiDate('.J-date');
        initTable();
    });

    function initTable() {
        var table;
        // 请求路径
        var requireUrl = {
            // edit: "{:url('score/apply')}",
            remove: "{:url('score/result_input_remove')}",
            list: "{:url('score/get_pass_score')}",
            apply: "{:url('score/public_result_apply')}",
        };
        var isStudent = getCookie('userClass') == 3 ? true : false;
        var isAdmin = getCookie('userClass') == 10 ? true : false;

        layui.use('table', function () {
            table = layui.table;
            table.render({
                elem: '#testTable',
                url: requireUrl.list,
                toolbar: '#toolbarDemo',
                title: '考试课程',
                totalRow: false,
                cols: [
                    [
                        {
                            type: 'checkbox',
                            fixed: 'left',
                            align: 'center',
                            minWidth: '50',
                        },
                        {
                            field: 'id',
                            title: 'ID',
                            align: 'center',
                            minWidth: '50',
                        },
                        {
                            field: 'term',
                            title: '学期',
                            align: 'center',
                            // width: '100',
                        },
                        {
                            field: 'coursename',
                            title: '课程',
                            align: 'center',
                            minWidth: '100',
                        },
                        {
                            field: 'studentname',
                            title: '学生',
                            align: 'center',
                            minWidth: '100',
                        },
                        {
                            field: 'usual_score',
                            title: '平时成绩',
                            align: 'center',
                            minWidth: '100',
                        },
                        {
                            field: 'test_score',
                            title: '考试成绩',
                            align: 'center',
                            minWidth: '100',
                        },
                        {
                            field: 'practice_score',
                            title: '实践成绩',
                            align: 'center',
                            minWidth: '100',
                        },
                        {
                            field: 'total_score',
                            title: '总成绩',
                            align: 'center',
                            minWidth: '120',
                        },
                        {
                            field: 'total_score',
                            title: '学分',
                            align: 'center',
                            minWidth: '120',
                            templet: function (d) {
                                // 绩点=分数/10-5
                                var scoreList = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
                                return scoreList[Math.floor(Math.random() * 4)];
                            }
                        }, {
                            field: 'total_score',
                            title: '绩点',
                            align: 'center',
                            minWidth: '120',
                            templet: function (d) {
                                // 绩点=分数/10-5
                                return (d.total_score / 10 - 5).toFixed(1) < 1 ? 0 : (d.total_score / 10 - 5).toFixed(1);
                            }
                        },
                        {
                            field: 'teachername',
                            title: '授课老师',
                            align: 'center',
                            minWidth: '100',
                        },
                        {
                            field: 'memo',
                            title: '备注',
                            align: 'center',
                            minWidth: '100',
                        },
                        {
                            field: 'create_time',
                            title: '时间',
                            align: 'center',
                            minWidth: '120',
                        },
                        {
                            fixed: 'right',
                            title: '操作',
                            toolbar: '#barDemo',
                            minWidth: '220',
                            hide: isAdmin,
                        },
                    ],
                ],
                id: 'testReload',
                page: true,
                parseData: function (res) {
                    // studentScores = res
                    var scoreList = [];
                    for (let index = 0; index < res.length; index++) {
                        var itemVal = (res[index].total_score / 10 - 5).toFixed(1) < 1 ? 0 : (res[index].total_score / 10 - 5).toFixed(1)
                        scoreList.push(Number(itemVal))
                    }

                    if (scoreList.length) {
                        var total = scoreList.reduce((x, y) => x + y)
                        $(".J-GPA").text((total / res.length).toFixed(1))
                    } else {
                        $(".J-GPA").text(0)
                    }

                    return {
                        code: 0,
                        msg: "",
                        count: res.total,
                        data: res.record
                    };
                },
            });

            //工具栏事件
            table.on('toolbar(tableOption)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'checkScore':
                        // todo
                        break;
                    case 'modifScore':
                        modifiScore();
                        break;
                }
            });

            //监听行工具事件
            table.on('tool(tableOption)', function (obj) {
                var data = obj.data;

                if (obj.event === 'modifScore') {
                    layerOpenInit(
                        '提交修改申请',
                        $('.J-temple'),
                        function (index) {
                            var data = $('.J-add-form').serialize();
                            data += '&create_time=' + new Date().Format('yyyy-MM-dd hh:mm:ss');

                            ajaxRequest('post', requireUrl.apply, data, function (data) {
                                data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                            });
                        },
                        function () {
                            ajaxRequest(
                                'get',
                                requireUrl.apply,
                                {
                                    id: data.id,
                                },
                                function (res) {
                                    form.val('formTest', res.data);

                                    upload.render({
                                        elem: '#test8',
                                        url: 'https://httpbin.org/post', //改成您自己的上传接口
                                        auto: false,
                                        accept: 'file',
                                        //,multiple: true
                                        bindAction: '#test9',
                                        done: function (res) {
                                            layer.msg('上传成功');
                                            console.log(res);
                                        },
                                    });
                                }
                            );
                        }
                    );
                }
            });
        });

        // 搜索重置操作
        var active = {
            reload: function () {
                var queryParams = serializeObject($('.J-search-form'));
                console.log(queryParams);
                //执行重载
                table.reload(
                    'testReload',
                    {
                        page: {
                            curr: 1, //重新从第 1 页开始
                        },
                        where: queryParams,
                    },
                    'data'
                );
            },
        };
        $('.J-search').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('.J-reload').on('click', function () {
            $('.J-query-params').val('');
            form.render('select');
            active.reload ? active.reload.call(this) : '';
        });
    }
</script>

{include file="my/footer"}